<?php 
session_start();
include("includes/session.php");
require 'includes/xls/Spreadsheet/Excel/Writer.php';
if(isset($_REQUEST['filename']))
{
	$filename = $_REQUEST['filename']; //chitiet/sotien/huy
	$marketing = $_REQUEST['marketing']; //chitiet/sotien/huy
	$tungay = strtotime($_REQUEST['tungay']) - 1; //echo $tungay."-";
	$denngay = strtotime($_REQUEST['denngay']) + 1; //echo $denngay;
	$export_file = $filename."_".$_REQUEST['tungay']."_".$_REQUEST['denngay'].".xls"; 
	if($marketing!=''){
		$export_file = '('.$marketing.')'.$export_file;
		$select = mysql_query("SELECT * FROM `contract` WHERE `date` > '$tungay' AND `date` < '$denngay' AND `marketing` = '$marketing'") or die ("21");
	}
	else{
		$select = mysql_query("SELECT * FROM `contract` WHERE `date` > '$tungay' AND `date` < '$denngay'") or die ("21");
	}
	$tktotalcty = mysql_num_rows($select);
	$thanhtien = '';
	$vatamount = '';
	$tongtien = '';
	if($filename == 'chitiet')
	{
		$workbook = new Spreadsheet_Excel_Writer();
		$workbook->setVersion(8, 'utf-8');
		$worksheet1 = &$workbook->addWorksheet('Chi Tiết');
		$worksheet1->freezePanes(array(1, 0));
		$worksheet1->setInputEncoding('UTF-8');
		$worksheet1->setColumn(0,1,4);
		$worksheet1->setColumn(2,2,40);
		$worksheet1->setColumn(3,3,14);
		$worksheet1->setColumn(4,4,40);
		$worksheet1->setColumn(5,5,20);
		$worksheet1->setColumn(6,6,15);
		$worksheet1->setColumn(7,7,30);
		$worksheet1->setColumn(8,8,10);
		$worksheet1->setColumn(9,9,10);
		$worksheet1->setColumn(10,10,25);
		$worksheet1->setColumn(11,11,14);
		$worksheet1->setColumn(12,12,8);
		$worksheet1->setColumn(13,13,8);
		$worksheet1->setColumn(14,14,10);
		$worksheet1->setColumn(15,15,10);
		$worksheet1->setColumn(16,16,10);
		$worksheet1->setColumn(17,17,10);
		$worksheet1->setColumn(18,18,10);
		$worksheet1->setColumn(19,19,8);
		$worksheet1->setColumn(20,20,8);
		$worksheet1->setColumn(21,21,8);
		$worksheet1->setColumn(22,22,8);
		$worksheet1->setColumn(23,23,12);
		$worksheet1->setColumn(24,24,9);
		$worksheet1->setColumn(25,25,10);
		$worksheet1->setColumn(26,26,10);
		$worksheet1->setColumn(27,27,10);
		$worksheet1->setColumn(28,28,15);
		$header = &$workbook->addFormat();$header->setBorder(1);$header->setHAlign('center');$header->setBold();$header->setColor('black');$header->setFgColor("grey");//background
		$contentCB = &$workbook->addFormat();$contentCB->setBold();$contentCB->setColor('black');$contentCB->setHAlign('center');
		$contentC = &$workbook->addFormat();$contentC->setColor('black');$contentC->setHAlign('center');
		$contentCI = &$workbook->addFormat();$contentCI->setItalic();$contentCI->setColor('black');$contentCI->setHAlign('center');
		$contentRB = &$workbook->addFormat();$contentRB->setBold();$contentRB->setColor('black');$contentRB->setHAlign('right');$contentRB->setNumFormat('0');
		$contentR = &$workbook->addFormat();$contentR->setColor('black');$contentR->setHAlign('right');
		$contentRI = &$workbook->addFormat();$contentRI->setItalic();$contentRI->setColor('black');$contentRI->setHAlign('right');
		$contentLB = &$workbook->addFormat();$contentLB->setBold();$contentLB->setColor('black');$contentLB->setHAlign('left');
		$contentL = &$workbook->addFormat();$contentL->setColor('black');$contentL->setHAlign('left');
		$contentLI = &$workbook->addFormat();$contentLI->setItalic();$contentLI->setColor('black');$contentLI->setHAlign('left');
		$worksheet1->write(0, 0, '#', $header);
		$worksheet1->write(0, 1, 'ID', $header);
		$worksheet1->write(0, 2, 'Tên công ty', $header);
		$worksheet1->write(0, 3, 'Mã số thuế', $header);
		$worksheet1->write(0, 4, 'Địa chỉ', $header);
		$worksheet1->write(0, 5, 'Người đại diện', $header);
		$worksheet1->write(0, 6, 'Chức vụ', $header);
		$worksheet1->write(0, 7, 'Số hợp đồng', $header);
		$worksheet1->write(0, 8, 'Ngày HĐ', $header);
		$worksheet1->write(0, 9, 'Ngày BB', $header);
		$worksheet1->write(0, 10, 'Tên sản phẩm', $header);
		$worksheet1->write(0, 11, 'Mẫu số', $header);
		$worksheet1->write(0, 12, 'Ký hiệu', $header);
		$worksheet1->write(0, 13, 'ĐV Tính', $header);
		$worksheet1->write(0, 14, 'Số lượng', $header);
		$worksheet1->write(0, 15, 'Đơn giá', $header);
		$worksheet1->write(0, 16, 'Thành tiền', $header);
		$worksheet1->write(0, 17, 'Tiền thuế', $header);
		$worksheet1->write(0, 18, 'Tổng tiền', $header);
		$worksheet1->write(0, 19, 'Số liên', $header);
		$worksheet1->write(0, 20, 'Số màu', $header);
		$worksheet1->write(0, 21, 'Bìa', $header);
		$worksheet1->write(0, 22, 'Ruột', $header);
		$worksheet1->write(0, 23, 'Kích cỡ', $header);
		$worksheet1->write(0, 24, 'Chống ký', $header);
		$worksheet1->write(0, 25, 'Giấy hủy', $header);
		$worksheet1->write(0, 26, 'Bản hủy', $header);
		$worksheet1->write(0, 27, 'Ngày nhập', $header);
		$worksheet1->write(0, 28, 'Người nhập', $header);
		while($rs = mysql_fetch_object($select))
		{
			for($i=1;$i<=$rs->product;$i++)
			{
				$pr = mysql_fetch_object(mysql_query("SELECT * FROM `product` WHERE `cid` = '$rs->id' and `order` = '$i' order by `order` asc"));
				if($pr->signed == '1'){$signed='Có';}else{$signed='Không';}
				$us = mysql_fetch_object(mysql_query("SELECT * FROM `user` where `username` = '$rs->userlog'"));
				$quantity = $quantity + $quantity1;
				$row = $row + 1;
				$thanhtien = $pr->quantity * $pr->price;
				$vatamount = $thanhtien * 10/100;
				$tongtien =  $thanhtien + $vatamount;
				$tientruocthue = $tientruocthue + $thanhtien;
				$tienthue = $tienthue + $vatamount;
				$tiensauthue = $tiensauthue + $tongtien;
				$giayhuy = $giayhuy + $pr->sohuy;
				$banhuy = $banhuy + $pr->banhuy;
				$worksheet1->write($row, 0, $row, $contentCB);
				$worksheet1->write($row, 1, $rs->id, $contentC);
				$worksheet1->write($row, 2, $rs->name, $contentLB);
				$worksheet1->write($row, 3, " ".$rs->tax, $contentRB);
				$worksheet1->write($row, 4, $rs->address, $contentL);
				$worksheet1->write($row, 5, $rs->director, $contentL);
				$worksheet1->write($row, 6, $rs->position, $contentL);
				$worksheet1->write($row, 7, $rs->contract, $contentLB);
				$worksheet1->write($row, 8, date('d/m/Y', $rs->date), $contentRB);
				$worksheet1->write($row, 9, $rs->datebb, $contentRB);
				$worksheet1->write($row, 10, $pr->product_name, $contentLB);
				$worksheet1->write($row, 11, $pr->form, $contentRB);
				$worksheet1->write($row, 12, $pr->serial, $contentRB);
				$worksheet1->write($row, 13, unit($pr->unit), $contentC);
				$worksheet1->write($row, 14, number_format($pr->quantity), $contentR);
				$worksheet1->write($row, 15, number_format($pr->price), $contentR);
				$worksheet1->write($row, 16, number_format($thanhtien), $contentR);
				$worksheet1->write($row, 17, number_format($vatamount), $contentR);
				$worksheet1->write($row, 18, number_format($tongtien), $contentRB);
				$worksheet1->write($row, 19, $pr->product_page, $contentC);
				$worksheet1->write($row, 20, $pr->color, $contentC);
				$worksheet1->write($row, 21, 'Indo', $contentC);
				$worksheet1->write($row, 22, bowel($pr->bowel), $contentC);
				$worksheet1->write($row, 23, $pr->size, $contentL);
				$worksheet1->write($row, 24, $signed, $contentC);
				$worksheet1->write($row, 25, number_format($pr->sohuy), $contentC);
				$worksheet1->write($row, 26, $pr->banhuy, $contentC);
				$worksheet1->write($row, 27, $rs->datelog, $contentR);
				$worksheet1->write($row, 28, $us->fullname, $contentR);
			}
		}
		$worksheet1->write($row+5, 2, 'Tổng số công ty:', $contentL);
		$worksheet1->write($row+6, 2, 'Tổng số hóa đơn:', $contentL);
		$worksheet1->write($row+7, 2, 'Tổng tiền trước thuế:', $contentL);
		$worksheet1->write($row+8, 2, 'Tổng tiền thuế:', $contentL);
		$worksheet1->write($row+9, 2, 'Tổng tiền sau thuế:', $contentL);
		$worksheet1->write($row+10, 2, 'Tổng số giấy hủy:', $contentL);
		$worksheet1->write($row+11, 2, 'Tổng số bản hủy:', $contentL);
		$worksheet1->write($row+5, 3, $tktotalcty, $contentRB);
		$worksheet1->write($row+6, 3, number_format($quantity), $contentRB);
		$worksheet1->write($row+7, 3, number_format($tientruocthue), $contentRB);
		$worksheet1->write($row+8, 3, number_format($tienthue), $contentRB);
		$worksheet1->write($row+9, 3, number_format($tiensauthue), $contentRB);
		$worksheet1->write($row+10, 3, number_format($giayhuy), $contentRB);
		$worksheet1->write($row+11, 3, number_format($banhuy), $contentRB);
		$workbook->send($export_file);
		$workbook->close();
	}
}
else
{
	die('File not found!');
}
?>
